﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static RhythmGame.Settings;

namespace RhythmGame
{
    public partial class NoteDrag : NoteTap
    {
        public NoteDrag()
        {
            InitializeComponent();
        }

        internal static void NextDrags(Game game, BeatMap map, List<NoteDrag> currentDrags, Element_info[] blockElements)
        {
            foreach (Element_info _element in blockElements)
            {
                if (_element.TypeId == 5)
                {
                    ElementDrag_info element = _element as ElementDrag_info;

                    NoteDrag dragNote = new NoteDrag();

                    // 设置出现时间
                    dragNote.AppearTime = element.ClickTime - Settings.Instance.DuringTime;

                    dragNote.JudgeTime = element.ClickTime;

                    // 设置轨道
                    dragNote.Track = element.X - 1;

                    dragNote.Location = new Point(0, 0);

                    dragNote.Width = element.Width * 40;
                    dragNote.Height = dragNote.Width / 6;
                    dragNote.BackColor = Settings.Instance.DragsColor;

                    // 根据 x 值设置水平位置
                    int maxTrackWidth = (int)(Settings.Instance.WinWidth * 0.9) / Settings.Instance.MaxKeysQuantity;
                    dragNote.Left = (int)(Settings.Instance.WinWidth * 0.05 + (maxTrackWidth - dragNote.Width) / 2 + maxTrackWidth * (element.X - 1));



                    // 初始垂直位置在窗口顶部
                    dragNote.Top = 0;

                    game.Controls.Add(dragNote);

                    currentDrags.Add(dragNote);
                    dragNote.BringToFront();
                }
                else
                {
                    continue;
                }
            }
        }
        
        internal static void DragsMove(Game game, List<NoteDrag> currentDrags, int currentTime)
        {
            foreach (var drag in currentDrags.ToList())
            {
                // 移除准备清除的音符
                if (game.dragsToRemove.Contains(drag))
                {
                    game.Controls.Remove(drag);
                    //game.SetLabelComboText("Remove Successful");
                    game.dragsToRemove.Remove(drag);
                    currentDrags.Remove(drag);
                    drag.Dispose();
                    continue;
                }

                if (currentTime >= drag.AppearTime)
                {
                    //MessageBox.Show(drag.AppearTime.ToString());
                    int elapsedTime = currentTime - drag.AppearTime;
                    int distance = Settings.Instance.DropSpeed * elapsedTime / Settings.Instance.UnitConversion;
                    //MessageBox.Show(distance.ToString());
                    drag.Top = distance;
                }

                if (drag.Top > Settings.Instance.WinHeight) // 当音符超过窗口高度
                {
                    // 判定为 Miss
                    //game.SetLabelDecideText("MISS");
                    Scoring.ApplyJudgement(game, "MISS", false);

                    // 将音符添加到待移除列表
                    game.dragsToRemove.Add(drag);
                }

            }
        }
        
        internal static void Judgement(Game game, List<NoteDrag> currentDrags, int currentTime, List<Tuple<Keys, int>> keysPress)
        {
            foreach (NoteDrag drag in currentDrags.ToList())
            {
                // 跳过已在待移除列表中的音符
                if (game.dragsToRemove.Contains(drag))
                    continue;

                int timeDifference = currentTime - drag.JudgeTime;

                bool keyPressed = false;

                if (Math.Abs(timeDifference) <= (int)Settings.JudgeStandard.Perfect) // 判定时间范围内
                {
                    // 检查对应轨道的按键是否处于按下状态
                    keyPressed = keysPress.Any(k => k.Item1 == Settings.Instance.KeysLocate[drag.Track]);
                    if (keyPressed)
                    {
                        // Critical Perfect
                        game.dragsToRemove.Add(drag);
                        Scoring.ApplyJudgement(game, "CRITICAL PERFECT", false);
                    }
                }               
            }
        }

        internal new static void NextTaps(Game game, BeatMap map, List<NoteTap> currentTaps, Element_info[] blockElements)
        {
            throw new NotSupportedException("不支持的操作：子类不可调用父类的 NextTaps 方法");
        }

        internal new static void TapsMove(Game game, List<NoteTap> currentTaps, int currentTime)
        {
            throw new NotSupportedException("不支持的操作：子类不可调用父类的 TapsMove 方法");
        }

        internal new static void Judgement(Game game, List<NoteTap> currentTaps, int currentTime, List<Tuple<Keys, int>> keysPress)
        {
            throw new NotSupportedException("不支持的操作：子类不可调用父类的 Judgement 方法");
        }
    }
}
